package com.spodrum.balloon;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;

public class BalloonGameThread extends Thread
{
	/** Are we running ? */
	private boolean mRun;
	
    /** Used to figure out elapsed time between frames */
    private long mLastTime;      
    
    /** Variables for the counter */
    private int frameSamplesCollected = 0;
    private int frameSampleTime = 0;
    private int fps = 0;
    
    /** Handle to the surface manager object we interact with */
    private SurfaceHolder mSurfaceHolder;

    /** How to display the text */
    private Paint textPaint;
    
    private BalloonView balloonView;
            
    public BalloonGameThread(SurfaceHolder surfaceHolder, BalloonView balloonView) 
    {
        mSurfaceHolder = surfaceHolder;
        this.balloonView = balloonView;
                    
       
        /** Initiate the text painter */
        textPaint = new Paint();
        textPaint.setARGB(255,0,0,255);
        //textPaint.setTextSize(32);
    }

    
    /**
     * The actual game loop!
     */
    public void run() 
    {
    	balloonView.init();
    	
        while (mRun) 
        {
            Canvas canvas = null;
            try {
                canvas = mSurfaceHolder.lockCanvas(null);
                //synchronized (mSurfaceHolder) 
                {   
                    long now = System.currentTimeMillis();
                    
                  	balloonView.update();
                    
                    balloonView.render(canvas);
                    
                    renderFPS(canvas, fps);
                    
                    if (mLastTime != 0) 
                    {
                    	
                    	//Time difference between now and last time we were here
                		int time = (int) (now - mLastTime);
                		frameSampleTime += time;
                		frameSamplesCollected++;
                		
                		//After 10 frames
                		if (frameSamplesCollected == 10) {
                			
                			//Update the fps variable
                    		fps = (int) (10000 / frameSampleTime);
                    		
                    		//Reset the sampletime + frames collected
                    		frameSampleTime = 0;
                    		frameSamplesCollected = 0;
                		}        		
                	}
                    
                    mLastTime = now;
                    

					try 
					{
						// send the thread to sleep for a short period
						// very useful for battery saving
						Thread.sleep(20);	
					} catch (InterruptedException e) {}
                }
            }finally 
            {
                if (canvas != null) 
                {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    private void renderFPS(Canvas canvas, int fps) 
    {
    	
    	canvas.drawText(fps + " fps", canvas.getWidth() -50, 20 , textPaint);
	}


	/**
     * So we can stop/pause the game loop
     */
    public void setRunning(boolean b) {
        mRun = b;
    }      
}
